package com.wanmait.movies.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.wanmait.movies.dao.MovieDao;
import com.wanmait.movies.vo.Movie;

/**
 * Servlet implementation class IndexServlet
 */
@WebServlet("/IndexServlet")
public class IndexServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public IndexServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		request.setCharacterEncoding("utf-8");
		
		response.setContentType("text/html; charset=utf-8");
		String action = request.getParameter("action");
		if (action==null||"begin".equals(action)) {
			doBegin(request, response);
		}
		if("ManegerBegin".equals(action)) {
			manegerBegin(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	protected void doBegin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 Movie[] lunbotu = MovieDao.getMovieDao().getLunBoMovies();
		 Movie[] mostLikeMovies = MovieDao.getMovieDao().getMostLikeMovie();
		 Movie[] highestScoreMovies = MovieDao.getMovieDao().getHighestScoreMovie();
		 Movie[] comingInMovies = MovieDao.getMovieDao().getComingInMovie();
		 request.setAttribute("lunbotu", lunbotu);
		 request.setAttribute("mostLikeMovies", mostLikeMovies);
		 request.setAttribute("highestScoreMovies", highestScoreMovies);
		 request.setAttribute("comingInMovies", comingInMovies);
 		 request.getRequestDispatcher("/index.jsp").forward(request, response);
	}
	
	//管理员退出到首页
	protected void manegerBegin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 HttpSession  session =request.getSession();
		 session.invalidate();
		 
		 Movie[] lunbotu = MovieDao.getMovieDao().getLunBoMovies();
		 Movie[] mostLikeMovies = MovieDao.getMovieDao().getMostLikeMovie();
		 Movie[] highestScoreMovies = MovieDao.getMovieDao().getHighestScoreMovie();
		 Movie[] comingInMovies = MovieDao.getMovieDao().getComingInMovie();
		 request.setAttribute("lunbotu", lunbotu);
		 request.setAttribute("mostLikeMovies", mostLikeMovies);
		 request.setAttribute("highestScoreMovies", highestScoreMovies);
		 request.setAttribute("comingInMovies", comingInMovies);
		 request.getRequestDispatcher("/index.jsp").forward(request, response);
	}
}
